//
//  LoadingViewProtocol.swift
//  Edaixi-iOS
//
//  Created by 刘树华 on 2017/12/5.
//  Copyright © 2017年 刘树华. All rights reserved.
//

import Foundation

enum LoadingViewStyle {
    case indeterminate , success ,failed
}

protocol LoadingViewInViewProtocol {
    
    func showLoadingViewInView(message:String,style:LoadingViewStyle?)
    func showLoadingViewInViewAfterHidden(message:String,style:LoadingViewStyle?,time:TimeInterval)
   
}
protocol LoadingViewInWindowProtocol {
     func showLoadingViewInWindow(message:String,style:LoadingViewStyle?)
    func showLoadingViewInWindowAfterHidden(message: String, style: LoadingViewStyle?, time: TimeInterval)
}

protocol LoadingViewHiddenProtocol {
    func hudHidden()
    func hudHiddenAfter(delay:TimeInterval)
}
//MARK ...HUD only covert View ,not all the screen
extension UIView : LoadingViewInViewProtocol,LoadingViewHiddenProtocol{
    
    func showLoadingViewInViewAfterHidden(message: String, style: LoadingViewStyle?, time: TimeInterval) {
        showLoadingViewInView(message: message, style: style)
        hudHiddenAfter(delay: time)
    }
    
    func showLoadingViewInView(message: String, style: LoadingViewStyle?) {
       var hud = MBProgressHUD(for: self)
        if hud == nil {
          hud = MBProgressHUD(view: self)
        }
        hud?.isSquare = true
        hud?.removeFromSuperViewOnHide = true
        if let model = style {
            switch model {
            case .indeterminate:
                hud?.isUserInteractionEnabled = true
                hud?.mode = .indeterminate
            case .success:
                hud?.isUserInteractionEnabled = false
                hud?.mode = .customView
                hud?.customView = UIImageView(imageName: "success (1)")
            case.failed:
                hud?.isUserInteractionEnabled = false
                hud?.mode = .customView
                hud?.customView = UIImageView(imageName: "close-cancel-128")
            }
        }else{
            hud?.isUserInteractionEnabled = true
            hud?.mode = .indeterminate
        }
         hud?.backgroundView.backgroundColor = UIColor.red
        hud?.label.text = message
        self.addSubview(hud!)
        hud?.show(animated: true)
    }
    func hudHidden() {
         let  hud = MBProgressHUD(for: self)
         hud?.hide(animated: true)
        
    }
    func hudHiddenAfter(delay: TimeInterval) {
        let  hud = MBProgressHUD(for: self)
        hud?.hide(animated: true, afterDelay: delay)
    }
}

extension UIViewController:LoadingViewInWindowProtocol,LoadingViewHiddenProtocol {
    
    func showLoadingViewInWindowAfterHidden(message: String, style: LoadingViewStyle?, time: TimeInterval) {
        showLoadingViewInWindow(message: message, style: style)
        hudHiddenAfter(delay: time)
    }
    
    func showLoadingViewInWindow(message: String, style: LoadingViewStyle?) {
        
       var hud = MBProgressHUD(for: WINDOW)
        if hud == nil {
           hud = MBProgressHUD(view: WINDOW)
        }
        hud?.isSquare = true
        hud?.removeFromSuperViewOnHide = true
        if let model = style {
            switch model {
            case .indeterminate:
                hud?.isUserInteractionEnabled = true
                hud?.mode = .indeterminate
            case .success:
                hud?.isUserInteractionEnabled = false
                hud?.mode = .customView
                hud?.customView = UIImageView(imageName: "success (1)")
            case.failed:
                hud?.isUserInteractionEnabled = false
                hud?.mode = .customView
                hud?.customView = UIImageView(imageName: "close-cancel-128")
            }
        }else{
            hud?.isUserInteractionEnabled = true
            hud?.mode = .indeterminate
        }
        hud?.backgroundView.backgroundColor = UIColor.red
        hud?.label.text = message
        WINDOW.addSubview(hud!)
        hud?.show(animated: true)
    }
    func hudHidden() {
        let  hud = MBProgressHUD(for: WINDOW)
        hud?.hide(animated: true)
    }
    func hudHiddenAfter(delay: TimeInterval) {
        let  hud = MBProgressHUD(for: WINDOW)
        hud?.hide(animated: true, afterDelay: delay)
    }
    
}

